package com.example.serviciocliente2;



import com.example.servicioservidor.IMiRemoto;




import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Portada extends Activity {
	IMiRemoto myRemoteService;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_portada);
        final TextView t1 = (TextView) findViewById(R.id.textView1);
        final Button b1 = (Button) findViewById(R.id.button1);
        final Button b2 = (Button) findViewById(R.id.button2);
        ServiceConnection conn = new ServiceConnection() {
			public void onServiceConnected(ComponentName name, IBinder service) {
				myRemoteService = IMiRemoto.Stub.asInterface(service);
			}
			public void onServiceDisconnected(ComponentName name) {
				// TODO Auto-generated method stub
			}
		};
        
        //notifico al servidor que existo
        Intent i = new Intent();
    	i.setClassName("com.example.servicioservidor","com.example.servicioservidor.Servicio");
    	i.putExtra("ruta paquete", "com.example.serviciocliente2");
    	i.putExtra("ruta clase", "com.example.serviciocliente2.servicioPortada");
    	Boolean b = bindService(i, conn, Context.BIND_AUTO_CREATE);
    	b1.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				String ret="ERROR"; 
				try {
					ret=myRemoteService.setRetonar();
					Toast.makeText(Portada.this,"toast del boton -> "+ret, Toast.LENGTH_LONG).show();
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				t1.setText(ret);
				
			}
		});
    	
    	b2.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				
				cerrar();
				
			}
		});
    	
    	
    	
    	
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_portada, menu);
        return true;
    }
    
    public void onDestroy(){
    	super.onDestroy();
    	
    }
    public void cerrar(){
    	this.finish();
    }
    
    
}
